package com.hxd.mall.utils.local

import com.hxd.mall.utils.open.ToastUtil
import java.text.SimpleDateFormat
import java.util.*

/**
 * Create by yinzhengwei on 2019-12-04
 * @Function
 */
object TimeUtil {
    private val DateLocal = ThreadLocal<SimpleDateFormat>()

    /**
     * 判断是否为今天(效率比较高)
     *
     * @param day 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    fun isToday(day: String): Boolean {
        val pre = Calendar.getInstance()
        pre.time = Date(System.currentTimeMillis())
        val cal = Calendar.getInstance()
        val date = getDateFormat()?.parse(day)
        cal.time = date
        if (cal.get(Calendar.YEAR) === pre.get(Calendar.YEAR)) {
            val diffDay = cal.get(Calendar.DAY_OF_YEAR) - pre.get(Calendar.DAY_OF_YEAR)
            if (diffDay == 0) {
                return true
            }
        }
        return false
    }

    fun isTaday(time: Long) = time >= getDailyStartTime() && time <= getDailyEndTime()

    fun getDateFormat(): SimpleDateFormat? {
        if (null == DateLocal.get()) {
            DateLocal.set(SimpleDateFormat("yyyy-MM-dd", Locale.CHINA))
        }
        return DateLocal.get()
    }

    /**
     *      * 获取指定某一天的开始时间戳
     *      *
     *      * @param timeStamp 毫秒级时间戳
     *      * @param timeZone  如 GMT+8:00
     *      * @return
     *      
     */
    fun getDailyStartTime(): Long {
        val calendar = Calendar.getInstance()
        calendar.timeZone = TimeZone.getTimeZone("GMT+8:00")
        calendar.timeInMillis = System.currentTimeMillis()
        calendar.set(Calendar.HOUR_OF_DAY, 0)
        calendar.set(Calendar.SECOND, 0)
        calendar.set(Calendar.MINUTE, 0)
        calendar.set(Calendar.MILLISECOND, 0)
        return calendar.timeInMillis
    }

    /**
     *      * 获取指定某一天的结束时间戳
     *      *
     *      * @param timeStamp 毫秒级时间戳
     *      * @param timeZone  如 GMT+8:00
     *      * @return
     *      
     */
    fun getDailyEndTime(): Long {
        val calendar = Calendar.getInstance()
        calendar.timeZone = TimeZone.getTimeZone("GMT+8:00")
        calendar.timeInMillis = System.currentTimeMillis()
        calendar.set(Calendar.HOUR_OF_DAY, 23)
        calendar.set(Calendar.SECOND, 59)
        calendar.set(Calendar.MINUTE, 59)
        calendar.set(Calendar.MILLISECOND, 999)
        return calendar.timeInMillis
    }


}